package com.ruoyi.web.controller.iot;

import com.ruoyi.MqttConsumer;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.IOTData;
import com.ruoyi.system.service.impl.IOTDataServiceImpl;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wang
 * @version 1.0
 * @data 2024/3/26 下午 8:09
 * @desc 描述信息
 */
@RestController
@RequestMapping("/iot")
public class IOT {

    @Autowired
    private IOTDataServiceImpl iotDataService;
    @GetMapping("/list")
    public AjaxResult getList(@RequestParam String p
            ,@RequestParam(required = false,defaultValue = "1") Long page
            ,@RequestParam(required = false,defaultValue = "10") Long pageSize
    ){
        return AjaxResult.success(iotDataService.getListByProperties(p,page,pageSize));
    }
    @PostMapping("/sendCmd")
    public AjaxResult sendCmd(@RequestBody Map<String,String> map){
        String cmd = map.get("cmd");
        String code = "esp32/command";
        MqttConsumer.publish(code,cmd);
        return AjaxResult.success("下发成功");
    }

    @GetMapping("/getData")
    public AjaxResult getData(@RequestParam String p){
        System.out.println(p);
        IOTData oneData = iotDataService.getOneData(p);
        return AjaxResult.success(oneData);
    }
    @GetMapping("/query-device-property")
    public AjaxResult getProperty(){
        List<IOTData> iotData = iotDataService.queryProperty();
        return AjaxResult.success("成功",iotData);
    }
}
